pimpas.DOM.Event = Class.create( );
pimpas.Extends(pimpas.DOM.Event, pimpas.Object);
pimpas.Extends(pimpas.DOM.Event, pimpas.ListenerSupport.prototype);
// listener
pimpas.DOM.Event.Listener = Class.create( );
pimpas.Extends(pimpas.DOM.Event.Listener.prototype, pimpas.Listener.prototype);
pimpas.Extends(pimpas.DOM.Event.Listener.prototype, 
   {
	   observer: null,
	   eventName: null,
	   element: null,
	   useCapture: null,
	   initialize: function(o, en, e, u) {
			this.id = pimpas.globalID++;
			this.log = new pimpas.Log();
			this.observer = o;
			this.eventName = en;
			this.element = pimpas.DOM.getElement( e );
			this.useCapture = u;
	   },
	   callBack: function(event) {
	   },
	   toString: function() {
			return "[DOM-EVENT-LISTENER]";   
	   }
   }
);
pimpas.Extends(pimpas.DOM.Event,
   {
	initialize: function() {
		this.id = pimpas.globalID++;
		this.log = new pimpas.Log();
		this.listeners = new Array();
	},
	fixEvent: function(e) {
		return e || event || window.event;
	},
	getElement: function(event) {
		event = this.fixEvent(event);
		return (event.target || event.srcElement);   
	},
	getKeyPressed: function(event) {
		event = this.fixEvent(event);
		return (event.keyCode || event.which);
	},
	isLeftClick: function(event) {
		event = this.fixEvent(event);
		return (((event.which) && (event.which == 1)) ||
				((event.button) && (event.button == 1)));
	},
	isRightClick: function(event) {
		event = this.fixEvent(event);
		return (((event.which) && (event.which == 2)) ||
				((event.button) && (event.button == 2)));
	},
	pointerX: function(event) {
		event = this.fixEvent(event);
		return event.pageX || (event.clientX + 
		  (document.documentElement.scrollLeft || document.body.scrollLeft));
	},
	pointerY: function(event) {
		event = this.fixEvent(event);
		return event.pageY || (event.clientY + 
		  (document.documentElement.scrollTop || document.body.scrollTop));
	},
	stop: function(event) {
		event = this.fixEvent(event);
		if (event.preventDefault) { 
		  event.preventDefault(); 
		  event.stopPropagation(); 
		} else {
		  event.returnValue = false;
		  event.cancelBubble = true;
		}
	},
	addListener: function(element, eventName, observer, useCapture) {
		var l = new pimpas.DOM.Event.Listener(observer, eventName, element, useCapture);
		
		this.registerBrowserListener( l );
		this.listeners.push( l );
	},
	registerBrowserListener: function( l ) {
		if(l.element.addEventListener)
	    	l.element.addEventListener(l.eventName, l.observer, l.useCapture);
		else if(l.element.attachEvent)
	    	l.element.attachEvent('on' + l.eventName, l.observer);
		else
			throw "Not implemented Yet";
	},
	unRegisterBrowserListener: function( l ) {
		
		if(l.element.removeEventListener)
			l.element.removeEventListener( l.eventName, l.observer, l.useCapture );
		else if(l.element.detachEvent)
			l.element.detachEvent('on' + l.eventName, l.observer);
		else
			throw "Not implemented Yet";
	},
	callListeners: function() {
		
	},
	removeListener: function(el, eventName, observer) {
		el = pimpas.DOM.getElement( el );
		var l = null;
		for(var i=0;i<this.listeners.length;i++)
			if(this.listeners[i].element == el &&
				this.listeners[i].eventName == eventName &&
				this.listeners[i].observer == observer) {
				l = this.listeners[i];
				break;
			}
		if(l != null) {
			this.listeners.remove( l );
			this.unRegisterBrowserListener( l );
		}
	 },
	 CLICK: "click",
	 FOCUS: "focus",
	 MOUSEMOVE: "mousemove",
	 KEYPRESS: "keypress",
	 KEYUP: "keyup",
	 KEYDOWN: "keydown",
	 BLUR: "blur",
	 SUBMIT: "submit",
	 MOUSEOVER: "mouseover",
	 MOUSEOUT: "mouseout",
	 MOUSEDOWN: "mousedown",
	 MOUSEUP: "mouseup",
	 LOAD: "load",
	 UNLOAD: "unload"
   }
);

pimpas.DOM.Event.initialize( );